package com.dameng.jianyouquan.interviewer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dameng.jianyouquan.R;
import com.dameng.jianyouquan.bean.ListWelfareBean;
import com.dameng.jianyouquan.api.NetWorkManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class InterviewerPublish3Activity extends AppCompatActivity {

    @BindView(R.id.rb_1)
    RadioButton rb1;
    @BindView(R.id.rb_2)
    RadioButton rb2;
    @BindView(R.id.et_wages)
    EditText etWages;
    @BindView(R.id.recycleView)
    RecyclerView mRecyclerView;
    @BindView(R.id.et_commission)
    EditText etCommission;
    @BindView(R.id.btn_next)
    Button btnNext;

    private String welfareID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interviewer_publish3);
        ButterKnife.bind(this);


        NetWorkManager.getService()
                .getWelfare()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ListWelfareBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ListWelfareBean listWelfareBean) {
                        int code = listWelfareBean.getCode();
                        if (code==200){
                            List<ListWelfareBean.DataBean> data = listWelfareBean.getData();

                            GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 3, GridLayoutManager.VERTICAL, false);
                            gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                            mRecyclerView.setLayoutManager(gridLayoutManager);
                            MyAdapter  mAdapter = new MyAdapter(data);
                            mRecyclerView.setAdapter(mAdapter);
                        }
                    }
                });

    }

    @OnClick({R.id.iv_back, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_next:
                Intent intent = getIntent();
                String postID = intent.getStringExtra("postID");
                String postName = intent.getStringExtra("postName");
                String industryID = intent.getStringExtra("industryID");
                String industryName = intent.getStringExtra("industryName");
                String jobtitle = intent.getStringExtra("jobtitle");
                String content = intent.getStringExtra("content");
                String persion = intent.getStringExtra("persion");

                String SettlementMethod="";
                if (rb1.isChecked()){
                    SettlementMethod="1";
                }else if (rb2.isChecked()){
                    SettlementMethod="2";
                }

                String wages = etWages.getText().toString().trim();
                String commission = etCommission.getText().toString().trim();

                if (TextUtils.isEmpty(wages)){
                    Toast.makeText(getApplicationContext(),"请填写基本工资",Toast.LENGTH_SHORT).show();
                    return;
                }


                Intent intent2 = new Intent(InterviewerPublish3Activity.this, InterviewerPublish4Activity.class);
                intent2.putExtra("postID",postID)
                        .putExtra("postName",postName)
                        .putExtra("industryID",industryID)
                        .putExtra("industryName",industryName)
                        .putExtra("jobtitle",jobtitle)
                        .putExtra("content",content)
                        .putExtra("persion",persion)
                        .putExtra("SettlementMethod",SettlementMethod)
                        .putExtra("wages",wages)
                        .putExtra("welfareID",welfareID)
                        .putExtra("commission",commission);
                startActivity(intent2);



                break;

        }
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {
        List<ListWelfareBean.DataBean> list=new ArrayList<>();
        private MyAdapter(List<ListWelfareBean.DataBean> data) {
            list=data;
        }

        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.item_profession, null);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder holder, int position) {

            holder.textView.setText(list.get(position).getParamName());

            holder.itemView.setOnClickListener(view -> welfareID=list.get(position).getParamId()+"");

        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    private class MyHolder extends RecyclerView.ViewHolder{

        TextView textView;

        private MyHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.tv_name);
        }
    }
}
